package tech.aistar.day07;

/**
 * 本类用来演示: 字符串拼接性能
 *    String的拼接(+)的性能远远低于StringBuilder的append
 *
 * @author: success
 * @date: 2022/7/26 1:46 下午
 */
public class StringPlusDemo {
    public static void main(String[] args) {
        append();

        plus();
    }

    public static void plus(){
        long before = System.currentTimeMillis();

        String str = "";

        for (int i = 0; i < 100000; i++) {
            str+=i;//str = str + i;
        }

        System.out.println("str+:"+(System.currentTimeMillis()-before)+"ms");
    }

    public static void append(){
        long before = System.currentTimeMillis();

        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < 100000; i++) {
           builder.append(i);
        }

        System.out.println("append:"+(System.currentTimeMillis()-before)+"ms");
    }


}
